package cn.manter.org.service.impl;

import cn.manter.basic.service.impl.BaseServiceImpl;
import cn.manter.basic.utils.MD5Utils;
import cn.manter.basic.utils.StrUtils;
import cn.manter.org.domain.Employee;
import cn.manter.org.mapper.EmployeeMapper;
import cn.manter.org.service.IEmployeeService;
import cn.manter.user.domain.Logininfo;
import cn.manter.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void save(Employee employee) {
        // 1.初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.save(employee);
        // 2.初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        // 3.将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());
    }
    // TODD
    @Override
    @Transactional
    public void update(Employee employee) {
        // 1.初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.update(employee);
        // 2.初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.update(logininfo);
        // 3.将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());
    }



    @Override
    @Transactional
    public void remove(Long id) {
        Employee employee = employeeMapper.loadById(id);
        logininfoMapper.remove(employee.getLogininfoId());
        employeeMapper.remove(id);


    }
    /**
     * @Title: initEmployee
     * @Description: 初始化employee
     * @Author: Mr.Zhao
     * @Version: 1.0
     * @Date:  2022/7/6 16:43
     * @Parameters: [employee]
     * @Return void
     */
    private void initEmployee(Employee employee) {
        // 用工具类进行加盐加密
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        // 吧加盐加密好的数据设置到employee表中
        employee.setSalt(salt);
        employee.setPassword(saltPassword);
    }

    /**
     * @Title: employee2Logininfo
     * @Description: 将employee的数据拷贝到logininfo中
     * @Author: Mr.Zhao
     * @Version: 1.0
     * @Date:  2022/7/6 16:44
     * @Parameters: [employee]
     * @Return cn.manter.user.domain.Logininfo
     */
    private Logininfo employee2Logininfo(Employee employee) {
        // 拷贝数据
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        // 设置用户类型为员工
        logininfo.setType(0);
        // 返回对象
        return logininfo;
    }




    @Override
    public List<Employee> loadByDeptId(Long department_id) {
        return employeeMapper.loadByDeptId(department_id);
    }
}